console.log('start web server...');



const express = require('express')
//const cors = require('cors');
const bodyParser = require('body-parser');
const req = require('express/lib/request');
const userLib = require("./objs/user")

const app = express()
const  port = 10000 




//app.use(cors());
app.use(bodyParser.json()); //这里指定参数使用 json 格式
app.use(bodyParser.urlencoded({
    extended: false
})); 

app.get('/', (req , res)=>{
    res.send('hello world.')
})
app.get('/abc', (req , res)=>{
    user = req.query.user 
    gender = req.query.gender 
    res.send('hello world.  ---- abc:'+ user +'--'+ gender )
})


function getRequest(req){
    var ps = {}
    queryPsKeys = Object.keys(req.query)
    queryPsKeys.forEach(k => {
        ps[k] = req.query[k];
    });
    bodyPsKeys = Object.keys(req.body);
    bodyPsKeys.forEach( k =>{
        ps[k] = req.body[k]
    }) 
    return ps
}
app.all('/abc',(req , res)=>{
    ps = getRequest(req)
    Object.keys(ps).forEach( k=>{
        v = ps[k]
        console.log( k+"="+v.toString())
    })
    res.send('hello world.  ---- abc')
})

app.post("/api/user/login", (req, res)=>{
    var params = getRequest(req)
    userLib.user.checkLogin( params.userCode , params.userPwd).then(loginRes=>{
        var sendData = JSON.stringify(loginRes);
        res.send(sendData)
    })
})

app.listen( port ,'0.0.0.0', ()=>{
//app.listen( port , ()=>{
        console.log('web server start success!')
})

console.log('started')
